const Router = require('koa-router')
const router = new Router()
const koaBody = require('koa-bodyparser');

const wr = require('../model/word_recite')

router.prefix('/word');

router.get('/recite/:username',async ctx=>{
    const username = ctx.params.username;
    const rows = await wr.GETRECITE(username);
    if(rows.recite_num <= rows.today_num){
        //当天学习词汇量大于复习量，拿到所有要复习的单词
        const new_num = rows.today_num-rows.recite_num;
        //获取到要新背的单词
        const rows_t = await wr.GETNEWWORDS(username,new_num)
        ctx.body = rows.rows_re.concat(rows_t)
        

    }else{
        ctx.body={msg:'查找有误'}
    }
})

router.post('/addtable',koaBody(),async ctx=>{
    const username = ctx.request.body.username;
    const data_inser = ctx.request.body.dataInsert;
    const rows = await wr.ADDTABLE(username,data_inser);
    ctx.body = rows;
})

//获取到今日应背的单词资源
router.post('/getword',koaBody(),async ctx=>{
    const username = ctx.request.body.username;
    const rows = await wr.GETWORD(username);
    ctx.body= rows;
})

router.post('/click',koaBody(),async ctx=>{
    const username = ctx.request.body.username;
    const id = ctx.request.body.word_id;
    const is = ctx.request.body.isKnow;
    const rows = await wr.CLICK(username,id,is);
    ctx.body = rows;
})

router.post('/complete',koaBody(),async ctx=>{
    const username = ctx.request.body.username;
    const rows = await wr.COMPLETE(username);
    ctx.body = rows
})

router.get('/todaylength/:username',async ctx=>{
    const username = ctx.params.username;
    const rows = await wr.GETTODAYLEN(username);
    ctx.body = rows;
})




module.exports = router;
